using System;
using System.Collections.Generic;
using System.Linq;
using VoiceAd.Models;

namespace VoiceAd.DAL
{
    public class EmailContentRepository : IEmailContentRepository, IDisposable
    {
        private dbVoiceAdDataContext context = new dbVoiceAdDataContext();

        public EmailContentRepository(dbVoiceAdDataContext db)
        {
            this.context = db;
        }

        public IEnumerable<tblEmailContent> GetList()
        {
            return context.tblEmailContents.ToList();
        }

        public tblEmailContent GetById(int lId)
        {
            return context.tblEmailContents.Where(x => x.Id == lId).FirstOrDefault();
        }

        public void InsEmailContent(tblEmailContent tblemailContent)
        {
            context.tblEmailContents.InsertOnSubmit(tblemailContent);
            context.SubmitChanges();
        }

        public void UpdEmailContent(tblEmailContent tblemailContent)
        {
            tblEmailContent obj = context.tblEmailContents.Where(x => x.Id == tblemailContent.Id).First();
            obj.Title = tblemailContent.Title;
            obj.Body = tblemailContent.Body;
            context.SubmitChanges();
        }

        public void DelEmailContent(int id)
        {
            tblEmailContent obj = context.tblEmailContents.Single(x => x.Id == id);
            context.tblEmailContents.DeleteOnSubmit(obj);
            context.SubmitChanges();
        }

        private bool disposed = false;

        protected virtual void Dispose(bool disposing)
        {
            if (!this.disposed)
            {
                if (disposing)
                {
                    context.Dispose();
                }
            }
            this.disposed = true;
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    }
}
